using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Reflection;
using System.Xml.Linq;

namespace XmlDB
{
    internal class Utils
    {
        internal static bool XmlAttNull(XAttribute att)
        {
            return att != null ? string.IsNullOrEmpty(att.Value) : true;
        }

        internal static object GetValueFromString(string strValue, Type retType)
        {
            if (retType.FullName == "System.String")
                return strValue;

            object retval = null;
            switch (retType.FullName)
            {
                case "System.Int32":
                    retval = Convert.ToInt32(strValue);
                    break;
                case "System.DateTime":
                    retval = Convert.ToDateTime(strValue);
                    break;
                case "System.Double":
                    retval = Convert.ToDouble(strValue);
                    break;
                case "System.Boolean":
                    retval = Convert.ToBoolean(strValue);
                    break;
                case "System.Decimal":
                    retval = Convert.ToDecimal(strValue);
                    break;
                case "System.Int16":
                    retval = Convert.ToInt16(strValue);
                    break;
                case "System.Int64":
                    retval = Convert.ToInt64(strValue);
                    break;
            }
            return retval;
        }

        internal static IEnumerable<XElement> GetDataElements(XDocument doc)
        {
   
            if (doc.Root == null)
                throw new Exception("Incorrect first node structure.");

            return doc.Root.Elements();
        }

        internal static T Map<T>(XElement xelement) where T : new()
        {
            T retval = new T();
            Type retType = typeof(T);
            foreach (PropertyInfo property in retType.GetProperties())
            {
                if (!Utils.XmlAttNull(xelement.Attribute(property.Name)))
                {
                    property.SetValue(retval, GetValueFromString(xelement.Attribute(property.Name).Value, property.PropertyType), null);
                }
            }
            return retval;
        }


        private static object GetNotNullValue(Type retType)
        {
            if (retType.FullName == "System.String")
                return string.Empty;

            object retval = null;
            switch (retType.FullName)
            {
                case "System.Int32":
                    retval = 0;
                    break;
                case "System.DateTime":
                    retval = DateTime.MinValue;
                    break;
                case "System.Double":
                    retval = 0;
                    break;
                case "System.Boolean":
                    retval = false;
                    break;
                case "System.Decimal":
                    retval = 0M;
                    break;
                case "System.Int16":
                    retval = 0;
                    break;
                case "System.Int64":
                    retval = 0;
                    break;
            }
            return retval;
        }

       

    }
}
